<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<azthink@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Example as ExampleModel;
use app\admin\model\File as FileModel;

/**
 * 示例控制器
 *
 * @author 李忠<azthink@qq.com>
 * @createAt 2018-09-14 14:49:03
 *
 */
class Example extends Admin {
    /**
     * 列表页面
     */
    public function index($title = null, $status = null) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = ExampleModel::lists($title, $status);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('data_status', config('data_status'));
        $this->assign('status', $status);
        $this->assign('title', $title);
        $this->setMeta('示例管理');
        return $this->fetch();
    }

    // 新增示例
    public function add() {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Example');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = ExampleModel::create($data)) {
                $this->log($data['id'], '新增示例成功');
                $this->success('新增示例成功', cookie('__forward__'));
            } else {
                $this->error('新增示例失败');
            }
        } else {
            $this->assign('upload_config', config('attachment_upload'));
            $this->assign('info', null);
            $this->setMeta('新增示例');
            return $this->fetch();
        }
    }

    // 更新示例
    public function edit($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Example');
            if (true !== $result) {
                $this->error($result);
            }
            if ($data = ExampleModel::update($data)) {

                $this->log($data['id'], '更新示例成功');
                $this->success('更新示例成功', cookie('__forward__'));
            } else {
                $this->error('更新示例失败');
            }

        } else {
            $info = ExampleModel::get($id);

            $attachment = FileModel::get($info['attachment']);
            $attachments = array();
            array_push($attachments, $attachment);

            $info['attachments'] = $attachments;
            $this->assign('info', $info);
            $this->assign('upload_config', config('attachment_upload'));
            $this->setMeta('更新示例');
            return $this->fetch('add');
        }
    }

    /**
     * 禁用
     * @param int $id
     */
    public function forbid($id) {
        if (ExampleModel::state($id, -1)) {
            $this->log($id, '禁用示例成功');
            $this->success('禁用示例成功');
        } else {
            $this->error('禁用示例失败');
        }
    }

    /**
     * 恢复
     * @param int $id
     */
    public function resume($id) {
        if (ExampleModel::state($id, -1)) {
            $this->log($id, '启用示例成功');
            $this->success('启用示例成功');
        } else {
            $this->error('启用示例失败');
        }
    }

    /**
     * 删除
     * @param int $id
     */
    public function delete($id) {
        if (ExampleModel::state($id, -1)) {
            $this->log($id, '删除示例成功');
            $this->success('删除示例成功');
        } else {
            $this->error('删除示例失败');
        }
    }


    public function editor() {
        if ($this->request->isPost()) {
            $files = $this->request->file();
            if ($files) {
                try {
                    $data = FileModel::upload(array_shift($files), config('editor_upload'));
                    $data['url'] = input('server.REQUEST_SCHEME') . '://' . input('server.SERVER_NAME') . $data['savepath'] . $data['savename'];
                    return json(array('error' => '0', 'url' => $data['url']));
                } catch (Exception $e) {
                    return json(array('error' => '1', 'message' => $e->getMessage()));
                }
            } else {
                return json(array('error' => '1', 'message' => '请选择上传文件'));
            }


        } else {
            return $this->fetch('editor');
        }
    }

}